package com.eralink.unionuhfreader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.bean.EPCBean;

import java.util.List;

public class InventoryTagListViewAdapter extends BaseAdapter {
    private final List<EPCBean> epcBeanList;
    private final LayoutInflater layoutInflater;

    public InventoryTagListViewAdapter(Context context, List<EPCBean> epcBeanList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.epcBeanList = epcBeanList;
    }

    @Override
    public int getCount() {
        return epcBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return epcBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TagListView tagListView;
        if (view == null){
            view = layoutInflater.inflate(R.layout.intentory_tag_item,parent,false);
            tagListView = new TagListView();
            tagListView.tvIndex = view.findViewById(R.id.tv_number);
            tagListView.tvReaderId = view.findViewById(R.id.tv_reader_id);
            tagListView.tvPC = view.findViewById(R.id.tv_pc);
            tagListView.tvEPC = view.findViewById(R.id.tv_epc);
            tagListView.tvRssi = view.findViewById(R.id.tv_rssi);
            tagListView.tvCount = view.findViewById(R.id.tv_count);
            tagListView.tvAnt = view.findViewById(R.id.tv_ant);
            view.setTag(tagListView);
        }
        else {
            tagListView = (TagListView) view.getTag();
        }

        tagListView.tvIndex.setText(String.valueOf(epcBeanList.get(position).getIndex() + 1));
        tagListView.tvReaderId.setText(String.valueOf(epcBeanList.get(position).getReaderId()));
        tagListView.tvPC.setText(epcBeanList.get(position).getPC());
        tagListView.tvEPC.setText(epcBeanList.get(position).getEPC());
        tagListView.tvRssi.setText(epcBeanList.get(position).getRssi());
        tagListView.tvAnt.setText(String.valueOf(epcBeanList.get(position).getAnt() + 1));
        tagListView.tvCount.setText(String.valueOf(epcBeanList.get(position).getCount()));
        return view;
    }

    class TagListView {
        public TextView tvIndex;
        public TextView tvReaderId;
        public TextView tvPC;
        public TextView tvEPC;
        public TextView tvAnt;
        public TextView tvRssi;
        public TextView tvCount;
    }
}
